package cn.com.wangluotongxin.merchants.rabbitmq;

import cn.com.wangluotongxin.common.exceptions.BadResponseException;
import cn.com.wangluotongxin.common.pojo.TenantInfo;
import cn.com.wangluotongxin.common.utils.TenantUtils;
import cn.com.wangluotongxin.merchants.database.MerchantsDatabaseService;
import cn.com.wangluotongxin.merchants.rabbitmq.listener.MerchantsRabbitmqConstants;
import cn.com.wangluotongxin.merchants.rabbitmq.pojo.GoodsInfoAuditEnd;
import cn.com.wangluotongxin.merchants.service.IMerchantsGoodsInfoMxService;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class RabbitmqListener {

    @Autowired
    private IMerchantsGoodsInfoMxService goodsInfoMxService;

    @Autowired
    private MerchantsDatabaseService databaseService;

    /**
     *  商品审核结束
     */
    @RabbitListener(queues = MerchantsRabbitmqConstants.GOODS_INFO_AUDIT_END, ackMode = "AUTO")
    public void goodsAuditEnd(GoodsInfoAuditEnd info){
        log.info("商品审核结果:{}", JSONObject.toJSONString(info));
        TenantInfo tenantInfo = databaseService.getTenantInfo(info.getTenantId());
        if(tenantInfo == null) {
            throw new BadResponseException("不存在的租户:tenant_id-{" + info.getTenantId() + "}");
        }
        TenantUtils.set(tenantInfo);
        goodsInfoMxService.auditEnd(info);
        TenantUtils.remove();
    }
}
